//coding=utf8
/**
 * @Author: bighu
 * @Created Time : 2022年05月17日 星期二 11时03分19秒
 * @File Name: RSA_client.java
 * @Description:
 */

import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
import java.util.Scanner;


public class RSA_client{
	public static void main(String[] args)throws Exception{
		KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
		gen.initialize(2048);
		KeyPair keyp = gen.genKeyPair();
		PublicKey pubkey = keyp.getPublic();
		PrivateKey prikey = keyp.getPrivate();
		byte[] pubEncBytes = pubkey.getEncoded();

		Scanner scan = new Scanner(System.in);
		String mess = scan.nextLine();


		//开始签名
		Signature sign = Signature.getInstance("SHA256withRSA");
		sign.initSign(prikey);
		sign.update(mess.getBytes());
		byte[] signInfo = sign.sign();
		System.out.println("签名结果为"+Base64.getEncoder().encodeToString(signInfo));

		Socket soc = new Socket("localhost",8000);
		OutputStream outputStream = soc.getOutputStream();
		outputStream.write(pubEncBytes);
		Thread.sleep(500);
		outputStream.write(mess.getBytes());
		Thread.sleep(500);
		outputStream.write(signInfo);

		soc.close();
		scan.close();

	}
}
